package com.retoc.graduationtravel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.retoc.graduationtravel.Utils.SnowFlake;
import com.retoc.graduationtravel.common.ApiRestResponse;
import com.retoc.graduationtravel.domain.City;
import com.retoc.graduationtravel.domain.Spot;
import com.retoc.graduationtravel.domain.SpotDetail;
import com.retoc.graduationtravel.domain.SpotImg;
import com.retoc.graduationtravel.domain.VO.SpotVO;
import com.retoc.graduationtravel.service.CityService;
import com.retoc.graduationtravel.service.SpotDetailService;
import com.retoc.graduationtravel.service.SpotImgService;
import com.retoc.graduationtravel.service.SpotService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/spot")
@RestController
public class SpotController {

    @Autowired
    SpotService spotService;

    @Autowired
    SpotImgService spotImgService;

    @Autowired
    SpotDetailService spotDetailService;

    @Autowired
    CityService cityService;

    @Autowired
    SnowFlake snowFlake;

    @GetMapping("/select")
    public ApiRestResponse<SpotVO> selectSpot(@RequestParam("spotId") Long spotId) {
        Spot spot = spotService.getById(spotId);
        QueryWrapper<SpotDetail> qwDetail = new QueryWrapper<>();
        qwDetail.eq("spot_id", spotId);
        List<SpotDetail> spotDetails = spotDetailService.list(qwDetail);
        QueryWrapper<SpotImg> qwImg = new QueryWrapper<>();
        qwImg.eq("spot_id", spotId);
        List<SpotImg> urlList = spotImgService.list(qwImg);
        SpotVO result = new SpotVO();
        BeanUtils.copyProperties(spot, result);
        result.setDetails(spotDetails);
        result.setImg(urlList);
        City city = cityService.getById(spot.getCityId());
        result.setCityName(city.getName());
        return ApiRestResponse.success(result);
    }

//    @PostMapping("/save")
//    public ApiRestResponse<Boolean> updateSpot(@Valid @RequestBody SpotSaveReq spotSaveReq) {
//        //新增文章
//        if (spotSaveReq.getId() == null) {
//            //向数据库写入spot基本信息
//            Spot spot = new Spot();
//            BeanUtils.copyProperties(spotSaveReq, spot);
//            Long spotId = snowFlake.nextId();
//            spot.setId(spotId);
//            spot.setDate(new Date());
//            Document doc = Jsoup.parse(spotSaveReq.getContent());
//            Element img = doc.select("img").first();
//            String imgUrl = "";
//            if (img != null) {
//                imgUrl = img.attr("src");
//            } else {
//                imgUrl = null;
//            }
//            spot.setImgUrl(imgUrl);
//            boolean save1 = spotService.save(spot);
//            //向数据库写入spot关联图片信息
//            Elements imgs = doc.select("img");
//            List<String> srcs = imgs.eachAttr("src");
//            for (int i = 0; i < srcs.size(); i++) {
//                SpotImg spotImg = new SpotImg();
//                spotImg.setSpotId(spotId);
//                spotImg.setImgUrl(srcs.get(i));
//                spotImgService.save(spotImg);
//            }
//            //向数据库写入spot内容信息
//            SpotDetail spotContent = new SpotDetail();
//            spotContent.setSpotId(spotId);
//            spotContent.setContent(spotSaveReq.getContent());
//            boolean save2 = spotDetailService.save(spotContent);
//            return ApiRestResponse.success(save1 && save2);
//        }
//        //修改文章
//        else {
//            Spot spot = new Spot();
//            BeanUtils.copyProperties(spotSaveReq, spot);
//            Document doc = Jsoup.parse(spotSaveReq.getContent());
//            Element img = doc.select("img").first();
//            String imgUrl = "";
//            if (img != null) {
//                imgUrl = img.attr("src");
//            } else {
//                imgUrl = null;
//            }
//            spot.setImgUrl(imgUrl);
//            boolean b = spotService.saveOrUpdate(spot);
//            //向数据库写入spot关联图片信息
//            //先清空该文章图片数据再添加
//            QueryWrapper<SpotImg> qw = new QueryWrapper<>();
//            qw.eq("spot_id", spotSaveReq.getId());
//            spotImgService.remove(qw);
//            Elements imgs = doc.select("img");
//            List<String> srcs = imgs.eachAttr("src");
//            for (String src : srcs) {
//                SpotImg spotImg = new SpotImg();
//                spotImg.setSpotId(spotSaveReq.getId());
//                spotImg.setImgUrl(src);
//                spotImgService.save(spotImg);
//            }
//            //向数据库写入spot内容信息
//            QueryWrapper<SpotDetail> qwc = new QueryWrapper<>();
//            qwc.eq("spot_id", spotSaveReq.getId());
//            spotDetailService.remove(qwc);
//            SpotDetail spotContent = new SpotDetail();
//            spotContent.setSpotId(spotSaveReq.getId());
//            spotContent.setContent(spotSaveReq.getContent());
//            spotDetailService.save(spotContent);
//            return ApiRestResponse.success();
//        }
//    }
//
//    @PostMapping("upload")
//    @ResponseBody
//    public Map upload(@RequestParam("img") MultipartFile file, HttpServletRequest request) {
//        String filename = file.getOriginalFilename();
//        assert filename != null;
//        String suffixName = filename.substring(filename.lastIndexOf("."));
//        String newFileName = UUID.randomUUID() + suffixName;
//        File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
//        File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
//        if (!fileDirectory.exists()) {
//            if (fileDirectory.mkdir()) {
//                throw new BusinessiException(ExceptionEnum.MKDIR_FAIL);
//            }
//        }
//        try {
//            file.transferTo(destFile);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        Map result = new HashMap();
//        try {
//            String fileURI = getHost(new URI(request.getRequestURL() + "")) + "/images/" + newFileName;
//            result.put("errno", 0);
//            Map dataMap = new HashMap();
//            dataMap.put("url", fileURI);
////            dataMap.put("alt",newFileName);
//            result.put("data", dataMap);
//            return result;
//        } catch (URISyntaxException e) {
//            result.put("errno", 1);
//            result.put("message", "上传失败");
//            return result;
//        }
//    }
//
//    private URI getHost(URI uri) {
//        URI effectiveURI;
//        try {
//            effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
//        } catch (URISyntaxException e) {
//            effectiveURI = null;
//        }
//        return effectiveURI;
//    }
}
